package com.wk.springboot.jdk8.methodRef;

import java.util.Comparator;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * @User admin
 * @Date 2020/11/19 15:53
 **/
public class MethodRefTest {

    public static void main(String[] args) {
//        instanceMethod();
//        classMethod();
//        classInsMethod();
        constructMethod();
    }

    private static void instanceMethod(){
        Consumer<String> consumer = x -> System.out.println(x);//正常Lambda表达式
        consumer.accept("正常lambda表达式");
        Consumer<String> consumer1 = System.out::println;//使用对象::实例方法语法
        consumer1.accept("对象::实例方法语法");

        People people = new People("tomcat");
        Supplier<String> supplier = () -> people.getName();//正常Lambda表达式
        System.out.println("supplier.get() = " + supplier.get()); //supplier.get() = tomcat

        Supplier<String> supplier1 = people::getName;//使用对象::实例方法语法
        System.out.println("supplier1.get() = " + supplier1.get()); //supplier1.get() = tomcat
    }

    private static void classMethod() {

        Comparator<Integer> comparator = (x,y)-> Integer.compare(x,y); //正常的lambda
        int compare = comparator.compare(5, 6);
        System.out.println("compare = " + compare);

        Comparator<Integer> comparator1 = Integer::compareTo; //类::静态方法
        int compare1 = comparator1.compare(6, 5);
        System.out.println("comparator1 = " + compare1);

    }

    private static void classInsMethod() {
        BiPredicate<String,String> biPredicate = (x,y)->x.equals(y); //biPredicate.test("abc","abc") = true
        System.out.println("biPredicate.test(\"abc\",\"abc\") = " + biPredicate.test("abc", "abc"));

        BiPredicate<String,String> biPredicate1 = String::equals; //biPredicate1.test("abc","adc") = false
        System.out.println("biPredicate1.test(\"abc\",\"adc\") = " + biPredicate1.test("abc", "adc"));
    }

    private static void constructMethod() {
        Supplier<People> supplier = ()->new People("jerry");//普通的构造方法 function.apply("jerry") = People{name='jerry'}
        System.out.println("supplier.get() = " + supplier.get());

        Supplier<People> supplier1 = People::new;//使用构造方法引用(调用的是无参构造方法) supplier1.get() = People{name='null'}
        System.out.println("supplier1.get() = " + supplier1.get());

        Function<String,People> function = (x)->new People(x);//调用有参构造方法 function.apply("jerry") = People{name='jerry'}
        System.out.println("function.apply(\"jerry\") = " + function.apply("jerry"));

        Function<String,People> function1 = People::new;//调用有参构造方法 function.apply("jerry") = People{name='jerry'}
        System.out.println("function1.apply(\"jerry\") = " + function1.apply("jerry"));
    }
    private static class People{
        /**
         * name
         **/
         private String name;

        public People() {
        }

        public People(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return "People{" +
                    "name='" + name + '\'' +
                    '}';
        }
    }
}
